package com.fjwt.gz.mgr.ctrl.lite.homepage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.db.dto.LiteHomepageDTO;
import com.fjwt.gz.db.entity.LiteHomepageEntity;
import com.fjwt.gz.db.vo.LiteHomepageVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.LiteHomepageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 首页接口类
 */
@Slf4j
@RestController
@RequestMapping("/api/liteHomePage")
public class LiteHomePageController extends CommonCtrl {

    @Autowired
    private LiteHomepageService homepageService;

    /**
     * 首页配置列表
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ApiRes getLiteHomepage() {
        Map map = getObject(Map.class);
        IPage<LiteHomepageEntity> page = homepageService.selectLiteHomepageList(getIPage(), map);
        IPage<LiteHomepageVO> pageVo = page.convert(e -> {
            LiteHomepageVO vo = BeanUtil.copyProperties(e, LiteHomepageVO.class);
            return vo;
        });
        log.info("pc端首页配置列表，请求地址：/api/liteHomePage，返回参数：{}", JSONUtil.toJsonStr(pageVo));
        return ApiRes.page(pageVo);
    }

    /**
     * 首页配置新增
     */
    @PostMapping("/save")
    public ApiRes liteHomepageSave() {
        LiteHomepageDTO dto = getObject(LiteHomepageDTO.class);
        Long sysUserId = getCurrentUser().getSysUserId();
        log.info("pc端首页配置新增，请求地址：/api/liteHomePage/save，请求参数：{}，sysUserId：{}", JSONUtil.toJsonStr(dto), JSONUtil.toJsonStr(sysUserId));
        homepageService.insert(dto,sysUserId);
        return ApiRes.ok();
    }

    /**
     * 首页配置修改
     */
    @PostMapping("/update")
    public ApiRes liteHomepageUpdate() {
        LiteHomepageDTO dto = getObject(LiteHomepageDTO.class);
        Long sysUserId = getCurrentUser().getSysUserId();
        log.info("pc端首页配置修改，请求地址：/api/liteHomePage/update，请求参数：{}，sysUserId：{}", JSONUtil.toJsonStr(dto), sysUserId);
        homepageService.updateLiteHomepage(dto,sysUserId);
        return ApiRes.ok();
    }

    /**
     * 首页配置信息
     */
    @PostMapping("/getInfo")
    public ApiRes liteHomepageGetInfo() {
        LiteHomepageDTO dto = getObject(LiteHomepageDTO.class);
        log.info("pc端首页配置详情，请求地址：/api/liteHomePage/getInfo，请求参数：{}", JSONUtil.toJsonStr(dto));
        LiteHomepageVO vo = homepageService.getInfoById(dto);
        log.info("pc端首页配置详情，请求地址：/api/liteHomePage/getInfo，返回结果：{}", JSONUtil.toJsonStr(vo));
        return ApiRes.ok(vo);
    }

    /**
     * 首页配置删除
     */
    @PostMapping("/remove")
    public ApiRes liteHomepageRemove() {
        LiteHomepageDTO dto = getObject(LiteHomepageDTO.class);
        if (Objects.isNull(dto.getHomepageId())) {
            return ApiRes.customFail("请求参数缺失！");
        }
        boolean flag = homepageService.removeById(dto.getHomepageId());
        if (flag) {
            return ApiRes.ok();
        }
        return ApiRes.customFail("操作失败！");
    }
}
